package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlansDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        return nursingPlanMapper.getAllNursingPlan();
    }

    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        nursingPlanDto.setStatus(0);
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.addNursingPlan(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        for (NursingProjectPlan nursingProjectPlans : nursingProjectPlan) {
            nursingProjectPlans.setPlanId(nursingPlan.getId());
            nursingPlanMapper.addNursingProjectPlan(nursingProjectPlans);
        }
    }

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> planVoPageResponse = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return planVoPageResponse;
    }

    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getNursingPlanById(id);
        List<NursingProjectPlan> nursingProjectPlans =  nursingPlanMapper.getNursingProjectPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlans);
        return nursingPlanVo;
    }

    @Override
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteNursingPlan(id);
        nursingPlanMapper.deleteNursingProjectPlan(id);
    }

    @Override
    public void modifyNursingPlanStatus(Long id, Integer status) {
        nursingPlanMapper.modifyNursingPlanStatus(id, status);
    }

    @Override
    public void modifyNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.modifyNursingPlan(nursingPlan);
        nursingPlanMapper.deleteNursingProjectPlan(nursingPlan.getId());
        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        for (NursingProjectPlan nursingProjectPlans : nursingProjectPlan) {
            nursingProjectPlans.setPlanId(nursingPlan.getId());
            nursingPlanMapper.addNursingProjectPlan(nursingProjectPlans);
        }
    }
}
